package com.zhenyi.liuxue.core;

import java.io.File;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.liuxue.common.model.LxCertificateItem;
import com.utils.PathUtils;
import com.utils.QNUtils;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;

public class ConvertUtils {

	String ak = "4TNY6RGqMNSA4BBJg-HxEJKtlVxQjL3JLuYKzeHg";
	String sk = "VaN2EfIEFU5rT2DW_0FIjmV6epqQJEXFAkqHDqUL";
	QNUtils qnUtils = new QNUtils(ak, sk, "superlink");

	public void run() {
		/**
		 * lx/admin/api/img
		 */
		qnUtils.project = "superlink";
		List<LxCertificateItem> items = LxCertificateItem.dao.findAll();
		for (LxCertificateItem lxCertificateItem : items) {
			if (lxCertificateItem.getIsVideo().equals(1)) {
				continue;
			}
			String content = lxCertificateItem.getContent();
			if (StrKit.isBlank(content)) {
				continue;
			}
			String[] imgs = content.split(";");
			int idx = 0;
			for (String url : imgs) {
				System.out.println(url);
				int iii = url.lastIndexOf(".");
				String fmt = url.substring(iii, url.length());
				String name = lxCertificateItem.getId() + "-" + idx + fmt;
				System.out.println(name);
				String path = PathUtils.getFile("logs", "lx", String.valueOf(lxCertificateItem.getId()), name);
				File file = new File(path);
				// if (!file.exists()) {
				// continue;
				// }
				long size = HttpUtil.downloadFile(url, file);
				if (size > 0) {
					String key = "{}/{}";
					key = StrFormatter.format(key, String.valueOf(lxCertificateItem.getId()), name);
					System.out.println(key);
					try {
						qnUtils.upload(file, key);
					} catch (Exception e) {
					}
					String newUrl = "http://res.superlinkplus.com/superlink/" + key;
					System.out.println(newUrl);
					content = content.replace(url, newUrl);
				}
				idx++;
			}
			System.out.println(content);
			lxCertificateItem.setContent(content);
			lxCertificateItem.update();
		}
		return;
	}
}
